package ui.listener;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JDialog;

import ui.panel.AdressPanel;
import ui.panel.CustomerPanel;
import ui.panel.RefJPanel;
import bean.Adress;

/**
 * Listener for the adress fields in the customer maintenance panel
 * @author Martin Schophaus
 *
 */
public class AdressFocusListener implements FocusListener{

	private RefJPanel panel;
	private String frame_title;
	private Adress adress;
	private boolean already_opened = false;
	
	/**
	 * Constructor sets the panel with which the listener works and the frame title
	 * @param panel The panel with which the listener interacts
	 * @param title
	 */
	public AdressFocusListener(RefJPanel panel, String title) {
		this.panel = panel;
		this.frame_title = title;
	}
	
	@Override
	public void focusGained(FocusEvent arg0) {
		if(!already_opened) {
			JDialog dialog = new JDialog(this.panel.frame, this.frame_title, true);
			try{
				AdressPanel frame = new AdressPanel(this.frame_title, this.adress, dialog);
			
			dialog.add(frame);
			dialog.setSize(300, 300);
			dialog.pack();
			dialog.setVisible(true);
			} catch(NullPointerException e){}
		}
		
		this.already_opened = !this.already_opened;
		
		if(panel.getClass() == CustomerPanel.class) {
			((CustomerPanel)this.panel).updateAdresses();
		}
	}

	@Override
	public void focusLost(FocusEvent arg0) {
		//((JTextArea)arg0.getSource()).transferFocus();
	}
	
	public void setAdress(Adress adress) {
		this.adress = adress;
	}

}